package com.aranandroid.customview.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.aranandroid.customview.R;
import com.blankj.utilcode.util.ThreadUtils;

import java.util.concurrent.TimeUnit;


/**
 * Created by Chen on 2017/6/4.
 * 录音控件
 */

public class RecordingView extends View implements Runnable{



    /**
     * 默认录音最低动画高度
     * 默认录音平静动画高度
     * 默认录音开始动画高度
     * 默认录音小控件宽度
     */
    private static final int DEFAULT_LOW_HEIGHT = 5;
    private static final int DEFAULT_DIF_HEIGHT = 3;
    private static final int DEFAULT_VIEW_WIDTH = 2;
    private static final int DEFAULT_VIEW_SPACING = 5;


    /**
     * 最低动画高度
     * 高度差
     * 小控件宽度
     * 小控件左右间距
     */
    private int lowHeight;
    private int difHeight;
    private int viewWidth;
    private int viewSpacing;


    // 最高标识
    private int highestMark = 0;
    private int highestMarkTemp = 0;
    private int offset = 4;

    // 画笔
    private Paint linePaint;


    public RecordingView(Context context) {
        this(context, null);
    }

    public RecordingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(attrs);
        initPaint();
    }

    private void getAttrs(AttributeSet attrs) {
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.RecordingView);
        lowHeight = ta.getDimensionPixelSize(R.styleable.RecordingView_lowHeight, sp2px(DEFAULT_LOW_HEIGHT));
        difHeight = ta.getDimensionPixelSize(R.styleable.RecordingView_difHeight, sp2px(DEFAULT_DIF_HEIGHT));
        viewWidth = ta.getDimensionPixelSize(R.styleable.RecordingView_viewWidth2, sp2px(DEFAULT_VIEW_WIDTH));
        viewSpacing = ta.getDimensionPixelSize(R.styleable.RecordingView_viewSpacing, sp2px(DEFAULT_VIEW_SPACING));

        ta.recycle();
    }



    private void initPaint() {
        linePaint = new Paint();
        // 头部文字
        //   #FF6363  #FFB300  #2BBC3A 颜色
        linePaint.setColor(Color.parseColor("#8F8DD1"));
        // 线宽
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setAntiAlias(true);


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //控件宽高
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 右边位置
        int leftMid = (getWidth() + viewSpacing) / 2;
        int topMid = (getHeight() - lowHeight) / 2 ;
        int rightMid = leftMid + viewWidth;
        int bottomMid = topMid + lowHeight;

        // 循环画线往右
        int iTemp = 0;
        int rightoffset = 0;
        for (int i = 0; i <10 ; i++) {
            //画矩形
            if(i>=offset){
                int topMidTemp = topMid;
                int bottomMidTemp = bottomMid;
                int i1 = iTemp - highestMark;
                if(i1>0){
                    topMidTemp = topMid - (i1 * difHeight);
                    bottomMidTemp = bottomMid + (i1 * difHeight);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    canvas.drawRoundRect(leftMid, topMidTemp, rightMid,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
                }else {
                    canvas.drawRect(leftMid, topMidTemp, rightMid,bottomMidTemp,linePaint);
                }
                // 往上移一个矩形距离
                leftMid = leftMid + (viewSpacing + viewWidth);
                rightMid = rightMid + (viewSpacing + viewWidth);
                rightoffset ++;
            }


            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }

        }

        // 循环画线往右
        for (int i = 0; i <8 ; i++) {
            int topMidTemp = topMid;
            int bottomMidTemp = bottomMid;
            int i1 = iTemp - highestMark;
            if(i1>0){
                topMidTemp = topMid - (i1 * difHeight);
                bottomMidTemp = bottomMid + (i1 * difHeight);
            }else {

            }
            //画矩形
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(leftMid, topMidTemp, rightMid,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
            }else {
                canvas.drawRect(leftMid, topMidTemp, rightMid,bottomMidTemp,linePaint);
            }
            // 往上移一个矩形距离
            leftMid = leftMid + (viewSpacing + viewWidth);
            rightMid = rightMid + (viewSpacing + viewWidth);
            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }
            rightoffset ++;
        }
        iTemp = 0;
        for (int i = 0; i <10 ; i++) {
            if(rightoffset>17){
                break;
            }
            int topMidTemp = topMid;
            int bottomMidTemp = bottomMid;
            int i1 = iTemp - highestMark;
            if(i1>0){
                topMidTemp = topMid - (i1 * difHeight);
                bottomMidTemp = bottomMid + (i1 * difHeight);
            }else {

            }
            //画矩形
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(leftMid, topMidTemp, rightMid,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
            }else {
                canvas.drawRect(leftMid, topMidTemp, rightMid,bottomMidTemp,linePaint);
            }
            // 往上移一个矩形距离
            leftMid = leftMid + (viewSpacing + viewWidth);
            rightMid = rightMid + (viewSpacing + viewWidth);
            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }
            rightoffset ++;
        }
        iTemp = 0;

        // 左边位置
        int leftoffset = 0;
        int rightMid2 = (getWidth() - viewSpacing) / 2;
        int leftMid2 = rightMid2 - viewWidth;

        // 循环画线往左
        for (int i = 0; i <10 ; i++) {
            //画矩形
            if(i>=offset){
                int topMidTemp = topMid;
                int bottomMidTemp = bottomMid;
                int i1 = iTemp - highestMark;
                if(i1>0){
                    topMidTemp = topMid - (i1 * difHeight);
                    bottomMidTemp = bottomMid + (i1 * difHeight);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    canvas.drawRoundRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
                }else {
                    canvas.drawRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp,linePaint);
                }
                // 往上移一个矩形距离
                leftMid2 = leftMid2 - (viewSpacing + viewWidth);
                rightMid2 = rightMid2 - (viewSpacing + viewWidth);
                leftoffset++;
            }

            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }


        }
        for (int i = 0; i <8 ; i++) {
            int topMidTemp = topMid;
            int bottomMidTemp = bottomMid;
            int i1 = iTemp - highestMark;
            if(i1>0){
                if (i1 == 4) {
                    i1 = 5;
                }
                topMidTemp = topMid - (i1 * difHeight);
                bottomMidTemp = bottomMid + (i1 * difHeight);
            }
            //画矩形
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
            }else {
                canvas.drawRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp,linePaint);
            }
            // 往上移一个矩形距离
            leftMid2 = leftMid2 - (viewSpacing + viewWidth);
            rightMid2 = rightMid2 - (viewSpacing + viewWidth);
            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }
            leftoffset++;

        }
        iTemp = 0;
        for (int i = 0; i <8 ; i++) {
            if(leftoffset>17){
                break;
            }
            int topMidTemp = topMid;
            int bottomMidTemp = bottomMid;
            int i1 = iTemp - highestMark;
            if(i1>0){
                if (i1 == 4) {
                    i1 = 5;
                }
                topMidTemp = topMid - (i1 * difHeight);
                bottomMidTemp = bottomMid + (i1 * difHeight);
            }
            //画矩形
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp, dp2px(4),dp2px(4),linePaint);
            }else {
                canvas.drawRect(leftMid2, topMidTemp, rightMid2,bottomMidTemp,linePaint);
            }
            // 往上移一个矩形距离
            leftMid2 = leftMid2 - (viewSpacing + viewWidth);
            rightMid2 = rightMid2 - (viewSpacing + viewWidth);
            if(i>4){
                iTemp--;
            }else {
                iTemp++;
            }
            leftoffset++;

        }

        // 刷新9次为一个周期
        if(highestMarkTemp<5){
            highestMark++;
        }else {
            highestMark--;
        }
        highestMarkTemp++;
        if(highestMarkTemp==10){
            highestMarkTemp = 0;
        }
        offset ++;
        if(offset>9){
            offset = 0;
        }
    }

    //dp to px
    protected int dp2px(int dpval) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpval, getResources().getDisplayMetrics());
    }

    //sp to px
    protected int sp2px(int dpval) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dpval, getResources().getDisplayMetrics());
    }

    @Override
    public void run() {
        ThreadUtils.executeByCpuAtFixRate(new ThreadUtils.Task<Object>() {
                @Override
                public Object doInBackground() throws Throwable {
                    invalidate();
                    return null;
                }

                @Override
                public void onSuccess(Object result) {

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onFail(Throwable t) {

                }
            },80, TimeUnit.MILLISECONDS);
    }
    //ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.5f,1,0.5f);//默认从（0,0）
    //                scaleAnimation.setDuration(3000);
    //                imageView4.startAnimation(scaleAnimation);
    //
}
